package net.googlecode.kharchenko.controllers;

import net.googlecode.kharchenko.domains.Person;
import net.googlecode.kharchenko.services.PersonService;
import net.googlecode.kharchenko.services.SecurityService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * Controller of user information page.
 */
@Controller
@RequestMapping("/user/{id}")
public class UserController {
    @Autowired
    private PersonService personService;

    @Autowired
    private SecurityService ss;


    protected static final Logger logger = Logger.getLogger(UserController.class);

    /**
     * Shows user's page with button to vote.
     *
     * @param userId user's id
     * @return user's page with button to vote.
     */
    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView handleRequest(@PathVariable("id") Long userId) {
        ModelAndView mv = new ModelAndView("user");
        logger.info("Get current person");
        Person user = personService.getPerson(ss.getCurrentUser());
        logger.info("Get user id");

        logger.info("Check if user id and person id equals");
        if (user.getId().equals(userId)) {
            logger.info("Redirect to home");
            return new ModelAndView("redirect:home");
        }
        Person prs;
        try {
            prs = personService.getPersonById(userId);
        } catch (javax.persistence.NoResultException ex) {

            return new ModelAndView("error404");
        }

        mv.addObject("info", prs);
        mv.addObject("groupmates", personService.getGroupMates(prs));

        logger.info("Check if vote was already given");
        if (personService.isVoteGiven(userId, user)) {
            mv.addObject("vote", 1);
        } else
            mv.addObject("vote", 0);

        logger.info("Return user information page");
        return mv;

    }

    /**
     * Give or giveout vote for user and show user's page.
     *
     * @param request request to server
     * @param userId  user's id
     * @return user's page
     */
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView handlePostRequest(HttpServletRequest request, @PathVariable("id") Long userId) {
        ModelAndView mv = new ModelAndView("user");

        logger.info("Get current person");
        Person user = personService.getPerson(ss.getCurrentUser());
        String rate = request.getParameter("rate");

        if (rate.equals("+1")) {
            logger.debug("Give vote for user id " + userId);
            personService.giveVote(userId, user);
        } else if (rate.equals("-1")) {

            logger.debug("Take out vote for user id " + userId);
            personService.takeAwayVote(userId, user);

        }
        mv.addObject("info", personService.getPersonById(userId));

        logger.info("Check if vote was already given");
        if (personService.isVoteGiven(userId, user)) {
            mv.addObject("vote", 1);
        } else
            mv.addObject("vote", 0);

        mv.addObject("groupmates", personService.getGroupMates(personService.getPersonById(userId)));

        return mv;

    }

}
